package com.cruise.h5.util;

import java.util.Date;

/**
 * @author ：Cruise
 * @version ：1.0
 * @Calss ：TimeUtile
 * @Description ：TODO
 * @date ：2021/8/7 下午11:48
 **/
public class TimeUtile {

    private static final long ONEDATE = 86400000; // 一天的时间差
    private static final long ONEHOUR = 3600000; // 一小时的时间差
    private static final long ONEMINUTE = 60000; // 一分钟的时间差

    /**
     *
     *
     * time类型 是 yyyy-MM-dd HH:mm:ss.s 不是的请转化成这个
     *
     *   time 未与当前时间相比的时间  time  就是未来的时间
     * @param time
     * @return
     */

    public static String putDate(Date time) {
        String dateStr = null;

        Date otheTime = time;
        Date nowTime = new Date();
        Long time10 = nowTime.getTime();
        Long time11 = otheTime.getTime();

        long timeDiff =  time10 - time11;

        if (timeDiff >= ONEDATE) {
            String someDate = someDate(timeDiff);
            return someDate;
        }
        if (timeDiff >= ONEHOUR) {
            return someHour(timeDiff);
        }
        if (timeDiff >= ONEMINUTE) {
            return someMinute(timeDiff);
        }

        return dateStr;
    }

    private static String someMinute(long timeDiff) {
        int dateNum = (int) (timeDiff / ONEMINUTE);
        System.out.println("dateNum  minute==  " + dateNum);
        String dateStr;
        if (dateNum < 10) {
            dateStr = "刚刚";
        } else if (dateNum < 30) {
            dateStr = "半小时前";
        } else {
            dateStr = "一个小时以内";
        }

        return dateStr;

    }

    /**
     *
     * 判断相差小时
     *
     * @param timeDiff
     * @return
     */

    private static String someHour(long timeDiff) {
        int dateNum = (int) (timeDiff / ONEHOUR);
        System.out.println("dateNum  someHour==  " + dateNum);
        String dateStr;
        if (dateNum < 12) {
            dateStr = getDate(dateNum, "小时前");
        } else {
            dateStr = "一天以内";
        }
        return dateStr;
    }

    /**
     *
     * 用于判断相差月数
     *
     * @param timeDiff
     * @return
     */
    private static String someDate(long timeDiff) {
        int dateNum = (int) (timeDiff / ONEDATE);
        System.out.println("dateNum  someDate==  " + dateNum);
        String dateStr;
        if (dateNum <= 15) {
            dateStr = getDate(dateNum, "天前");
        } else if (dateNum <= 30 && dateNum > 15) {
            dateStr = "一个月内";
        } else if (dateNum < 60) {
            dateStr = "两个月内";
        } else {
            dateStr = "很久以前";
        }
        return dateStr;
    }

    /**
     * 组装提示
     *
     * @param index
     * @param str
     * @return
     */
    private static String getDate(int index, String str) {
        return String.format("%s%s", enNum2CnNum(String.valueOf(index)), str);
    }

    /**
     *
     * 数字转化成中国的一二三
     *
     *
     * @param num
     * @return
     */
    private static String enNum2CnNum(String num) {
        String[] CnNum = new String[] { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
        //自己最大值 只需要 在这里添加就行了 其他代码不用改 例如到 亿
        String[] Cn10Num = new String[] { "十", "百", "千","万" };
        String CnStr = "";

        try {
            char[] strArray = num.toCharArray();
            int length = strArray.length;
            for (int i = 0; i < strArray.length; i++) {
                if (length > 1 && ((char2Int(strArray[i])) != 0)) {
                    CnStr += (CnNum[char2Int(strArray[i])] + Cn10Num[length - 2]);
                } else {
                    CnStr += CnNum[char2Int(strArray[i])];
                }
                length--;
            }
            // 处理多个零字段
            CnStr = CnStr.replaceAll("零零*", "零");
            // 处理最后一个是零
            if (CnStr.lastIndexOf("零") + 1 == CnStr.length()) {
                CnStr = CnStr.replaceAll("零$", "");
            }
            // 处理十几
            if (CnStr.contains("十") && (CnStr.length() == 3 || CnStr.length() == 2)) {
                CnStr = CnStr.replaceAll("^一", "");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return CnStr;

    }
    /**
     * 获取单个的String
     *
     * @param cha
     * @return
     */
    private static Integer char2Int(char cha) {
        return Integer.valueOf(cha + "");
    }

    public static void main(String[] args) {
        System.out.println("====  " + putDate(new Date()));
//		System.out.println(enNum2CnNum("10000"));

    }
}
